import sys
import time
import json
import random

def generate_metrics(epoch):
    return {
        'epoch': epoch,
        'mae': round(random.uniform(0.1, 1.0), 4),
        'mmre': round(random.uniform(0.05, 0.5), 4),
        'mdae': round(random.uniform(0.1, 0.8), 4),
        'pred': round(random.uniform(85.0, 95.0), 2)
    }

def main():
    if len(sys.argv) < 2:
        print("Missing max_epochs argument", file=sys.stderr)
        return

    try:
        max_epochs = int(sys.argv[1])
    except ValueError:
        print("Invalid max_epochs", file=sys.stderr)
        return

    for epoch in range(1, max_epochs + 1):
        metrics = generate_metrics(epoch)
        print('1')
        print("METRICS_JSON:", json.dumps(metrics), flush=True)  # flush=True 确保立即输出
        time.sleep(1)  # 模拟延迟

if __name__ == '__main__':
    main()
